package com.cxm.weilai.result;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.io.Serializable;

/**
 * @program: weilai-cloud
 * @description: 结果集
 * @author: ChenMan
 * @create: 2019-03-27 14:50
 */
@Data
public class ResultInfo<T> implements Serializable {
  /**
   * 数据
   */
  private T data;
  /**
   * 信息
   */
  private String message;
  /**
   * 响应码
   */
  private String code;

  public ResultInfo() {
    super();
  }

  public ResultInfo(ResponseInfo responseInfo) {
    this.code = responseInfo.getCode();
    this.message = responseInfo.getMsg();
  }

  public ResultInfo(ResponseInfo responseInfo, T data) {
    this.code = responseInfo.getCode();
    this.message = responseInfo.getMsg();
    this.data = data;
  }

  public static JSONObject OK(Object data) {
    if (null == data) {
      return (JSONObject) JSONObject.toJSON(new ResultInfo(ResponseInfo.SUCCESS));
    }
    return (JSONObject) JSONObject.toJSON(new ResultInfo(ResponseInfo.SUCCESS, data));
  }

  public static JSONObject error(Object data) {
    if (null == data) {
      return (JSONObject) JSONObject.toJSON(new ResultInfo(ResponseInfo.ERROR));
    }
    return (JSONObject) JSONObject.toJSON(new ResultInfo(ResponseInfo.ERROR,data));
  }

  public static JSONObject jsonResult(ResponseInfo responseInfo) {
    return (JSONObject) JSONObject.toJSON(new ResultInfo(responseInfo));
  }

  public static JSONObject jsonResult(Object data, ResponseInfo responseInfo) {
    return (JSONObject) JSONObject.toJSON(new ResultInfo(responseInfo,data));
  }

}
